package com.bnyy.medicalHousekeeper.moudle.message.fragment;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bnyy.medicalHousekeeper.base.BaseFragmentImpl;
import com.bnyy.medicalHousekeeper.moudle.message.adapter.NewGroupChatAdapter;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.bnyy.message.bean.NewChatGroup;
import com.bnyy.medicalHousekeeper.R;
import com.littlejerk.rvdivider.builder.XGridBuilder;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.simple.SimpleMultiListener;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/11/8
 **/
public class NewGroupChatFragment extends BaseFragmentImpl {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.iv_clear)
    ImageView ivClear;

    private NewGroupChatAdapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_message_new_group_chat;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        ivClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etSearch.setText("");
                refreshLayout.autoRefresh();
            }
        });

        etSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    Editable text = etSearch.getText();
                    if (!TextUtils.isEmpty(text)) {
                        refreshLayout.autoRefresh();
                        return true;
                    }
                }
                return false;
            }
        });
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                ivClear.setVisibility(TextUtils.isEmpty(s) ? View.GONE : View.VISIBLE);
            }
        });

        refreshLayout.setOnMultiListener(new SimpleMultiListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                index = 1;
                getNewChatGroups(new BaseObserverImpl<ArrayList<NewChatGroup>>() {
                    @Override
                    public void onSuccess(ArrayList<NewChatGroup> newChatGroups) {
                        super.onSuccess(newChatGroups);

                        if (adapter == null) {
                            recyclerView.setLayoutManager(new GridLayoutManager(mContext, 2));
                            recyclerView.addItemDecoration(new XGridBuilder(mContext)
                                    .setSpacing(12F)
                                    .setColor(ContextCompat.getColor(mContext, R.color.gray_background))
                                    .build());
                            adapter = new NewGroupChatAdapter(mContext);
                            recyclerView.setAdapter(adapter);
                        }
                        adapter.refresh(newChatGroups);
                    }
                });
            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                index++;
                getNewChatGroups(new BaseObserverImpl<ArrayList<NewChatGroup>>() {
                    @Override
                    public void onSuccess(ArrayList<NewChatGroup> newChatGroups) {
                        super.onSuccess(newChatGroups);

                        adapter.loadMore(newChatGroups);
                    }
                });
            }
        });
    }

    private int index = 1, size = 20;

    private void getNewChatGroups(BaseObserverImpl<ArrayList<NewChatGroup>> observer) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("page_index", index);
        params.put("page_size", size);
        if (!TextUtils.isEmpty(etSearch.getText())) {
            params.put("keyword", etSearch.getText().toString());
        }
        requestManager.request(requestManager.mMessageRetrofitService.getNewGroupChats(RequestManager.getJsonRequestBody(params)),
                new BaseObserverImpl<ArrayList<NewChatGroup>>(refreshLayout) {
                    @Override
                    public void onSuccess(ArrayList<NewChatGroup> newChatGroups) {
                        super.onSuccess(newChatGroups);

                        refreshLayout.setEnableLoadMore(newChatGroups.size() >= 20);

                        observer.onSuccess(newChatGroups);
                    }
                });
    }

    @Override
    public void onResume() {
        super.onResume();
        if (adapter == null) {
            refreshLayout.autoRefresh();
        }
    }
}





















